package com.kevin.util;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.lang.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

public class CacheUtil {
    private static final String REPLACE_SEPARATOR = "kevnivvliss";

    public static String getCachePath() {
        String path = "不能获取！";
        path = ConfigProperties.cacheConfig.getPath();
        return path;
    }

    public static void put(String key, Object value) {
        String propertyValue = JSON.toJSONString(value, SerializerFeature.WriteClassName);
        propertyValue = tranJsonStr(propertyValue);
        ConfigProperties.cacheConfig.setProperty(key, propertyValue);
        save();
    }

    /**
     * json字符转义（分号为config分隔符）
     * 
     * @Title tranJsonStr
     * @author 吕凯
     * @date 2016年9月22日 下午5:54:35
     * @param propertyValue
     * @return String
     */
    private static String tranJsonStr(String propertyValue) {
        return propertyValue.replace(";", REPLACE_SEPARATOR);
    }

    @SuppressWarnings("unchecked")
    public static <T> T get(String key) throws Exception {
        String configTableListStr = ConfigProperties.cacheConfig.getString(key);
        Object jsonTables = null;
        if (!StringUtils.isBlank(configTableListStr)) {
            configTableListStr = parseJosnStr(configTableListStr);
            jsonTables = JSON.parse(configTableListStr);
        }
        return jsonTables != null ? (T) jsonTables : null;
    }

    /**
     * 特殊字符还原
     * 
     * @Title parseJosnStr
     * @author 吕凯
     * @date 2016年9月22日 下午5:53:41
     * @param configTableListStr
     * @return String
     */
    private static String parseJosnStr(String configTableListStr) {
        configTableListStr = configTableListStr.replace(REPLACE_SEPARATOR, ";");
        return configTableListStr;
    }

    public static String getString(String key) {
        String configTableListStr = ConfigProperties.cacheConfig.getString(key);
        return configTableListStr;
    }

    public static <T> List<T> getList(String key, Class<T> classZ) throws Exception {
        String configTableListStr = ConfigProperties.cacheConfig.getString(key);
        List<T> jsonTables = new ArrayList<>();
        if (!StringUtils.isBlank(configTableListStr)) {
            configTableListStr = parseJosnStr(configTableListStr);
            jsonTables.addAll((List<T>) JSON.parseArray(configTableListStr, classZ));
//            JSONArray jsonArrays = (JSONArray) JSON.parse(configTableListStr);
//            for (int i = 0; i < jsonArrays.size(); i++) {
//                T sumJson = JSONObject.parseObject(jsonArrays.get(i).toString(), classZ);
//                jsonTables.add(sumJson);
//            }
        }
        return jsonTables;
    }

    public static void remove(String key) {
        ConfigProperties.cacheConfig.clearProperty(key);
    }

    public static void shutdown() {
        save();
    }

    private static void save() {
        try {
            ConfigProperties.cacheConfig.save();
        } catch (ConfigurationException e) {
            e.printStackTrace();
        }
    }

    public static boolean canUse() {
        boolean flag = false;
        return flag;
    }

}
